#ifndef _GUISYSTEM_H_
#define _GUISYSTEM_H_

#include <map>

#include "glfw.h"

#include "texture.h"
#include "input.h"

typedef unsigned int GuiId;

enum GuiEvent
{
    GE_NONE = 0,
    GE_CLICKED,
    GE_ITEMSELECTED
};

/*
Some things:
    - coordinate system origin is at the lower left corner
    - coordinates are resolution independent, ranging from 0.0 to 1.0
    - GUI element x & y coordinates define the bottom left corner of the element

*/

class GuiSystem
{
public:
    GuiSystem();
    ~GuiSystem();

    void addStatic(GuiId elementId, float x, float y, float w, float h,
                   Texture& tex);
    void addButton(GuiId elementId, float x, float y, float w, float h,
                   Texture& tex);

    void addSelectBox(GuiId elementId, float x, float y, float w, float h,
                      Texture& tex, int nOptions, int selected = 0,
                      float realH = 0);

    float widthToRel(float w);
    float heightToRel(float h);

    // x & y are in window coordinates (NOT resolution independent)
    bool isPosInElement(float x, float y, GuiId elementId);

    void enable(GuiId elementId);
    void disable(GuiId elementId);

    GuiEvent interact(const Input& input, GuiId* elementId, int* data);

    // This should only be called by view
    void draw();

private:

	enum GuiElementType
	{
		GET_STATIC = 0,
		GET_BUTTON,
		GET_SELECTBOX
	};

	class GuiElement
	{
    public:
		GuiElementType _type;
		bool _enabled;
		float _x, _y, _w, _h;
		Texture* _tex;
		void* _extra;
	};

    class SelectBoxExtra
    {
    public:
        SelectBoxExtra()
        {
            _nOptions = 0;
            _selected = 0;
            _realH = 0;
        }

        int _nOptions;
        int _selected;
        float _realH;
    };

    int _wndW, _wndH;

    std::map<GuiId, GuiElement> _elements;

};

#endif // _GUISYSTEM_H_
